home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / V_ARRAY.ARJ / PRIMES.C next >
C/C++ Source or Header  |  1991-09-06  |  2KB  |  76 lines

  1. #include "vir.h"
  2. #include <io.h>
  3. #include <dos.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. Vdcl(long,primes,4096,3);                /* Declare virtual */
  8.                             /* array of longs  */
  9.  
  10. int c_break(void)                    /* Close down Vfile*/
  11.   {                            /* on ^BREAK just  */
  12.   Vstop(primes);                    /* like hitting a  */
  13.   printf("\n");                        /* key so that data*/
  14.   return 0;                        /* is not lost!    */
  15.   }                            /* ^BREAK shouldn't*/
  16.                             /* be used!        */
  17. main()
  18.   {
  19.   long a;
  20.   long p;
  21.   long n;
  22.   ldiv_t test;
  23.   long divsr;
  24.  
  25.   ctrlbrk(c_break);                    /* Set up to handle*/
  26.                             /* ^C & ^BREAK     */
  27.  
  28.   Vinit(primes,"primes.dat",0,VCREATopt|VWRITEopt);    /* Initialize Vfile*/
  29.  
  30.   if(VErr(primes)==VFILEerr) return 4;            /* Check for error */
  31.  
  32.  
  33.     {/*** Get last prime if it exists ***/
  34.     long length;
  35.     length=filelength(VFile(primes));        /* Check length    */
  36.     if(length>3L)                    /* If this is a    */
  37.       {                        /* restart, get    */
  38.       length/=sizeof(long);                /* the last prime  */
  39.       p=V_(primes,length-1L)+2L;            /* number written  */
  40.       a=length;                    /* pick up where   */
  41.       }                        /* we left off.    */
  42.     else                        /* Otherwise,      */
  43.       {                        /* initialize file.*/
  44.       p=5L;a=2L;                      /* Starting point  */
  45.       V_(primes,0L)=2L;                /* Seed prime 2    */
  46.       V_(primes,1L)=3L;                /* Seed prime 3    */
  47.       }
  48.     }
  49.  
  50.   for(;p<1999999999L;p+=2)   /* Look for primes up to 2Gig... (LOTS!) */
  51.     {
  52.     for(n=1L;n<a;n++)                /* For each candi- */
  53.       {                        /* date, divide by */
  54.       test=ldiv(p,divsr=V_(primes,n));        /* each prime up to*/
  55.       if(test.quot<divsr) break;            /* it's square root*/
  56.       if(test.rem) continue;            /* until it divides*/
  57.       goto COMPOSITE_NUMBER;            /* evenly. If it   */
  58.       }                        /* divides evenly  */
  59.                             /* It is not prime!*/
  60.     printf("%li\r",p);                /* Show each new   */
  61.                             /* prime number as */
  62.     V_(primes,a)=p;                    /* it is added to  */
  63.     if(kbhit()) goto QUIT;                /* the array until */
  64.     a++;                        /* a key is hit.   */
  65.                             /* Stop examining  */
  66.     COMPOSITE_NUMBER:;                /* as soon as a    */
  67.     }                        /* number proves to*/
  68.                             /* be composite.   */
  69.   QUIT:                            /* When a key is   */
  70.   printf("\n");                        /* hit, leave the  */
  71.   Vstop(primes);                    /* last prime on   */
  72.                             /* Screen and close*/
  73.   return 0;                        /* down the V-File.*/
  74.   }
  75.  
  76.